package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.*;
import com.ruoyi.fly.domain.vo.ReimbursementsSearchVo;
import com.ruoyi.fly.domain.vo.ReimbursementsView;
import com.ruoyi.fly.domain.vo.ReimbursementsVo;
import com.ruoyi.fly.mapper.*;
import com.ruoyi.fly.service.ReimbursementsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReimbursementsServiceImpl implements ReimbursementsService {

    @Autowired
    private ReimbursementsMapper reimbursementsMapper;

    @Autowired
    private AppletMemberMapper appletMemberMapper;

    @Autowired
    private AgentStoreCarMapper agentStoreCarMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Override
    public Reimbursements selectReimbursementsById(String id) {
        return reimbursementsMapper.selectReimbursementsById(id);
    }

    @Override
    public List<ReimbursementsView> selectReimbursementsList(ReimbursementsSearchVo reimbursementsSearchVo) {
        Long userId = SecurityUtils.getUserId();
        List<ReimbursementsView> reimbursementsViewList =new ArrayList<>();
        if(userId!=1){
            List<AgentStoreCar> agentStoreCars = agentStoreCarMapper.selectAgentStoreCarByUserId(userId.toString());
            for(AgentStoreCar agentStoreCar : agentStoreCars){
                if(agentStoreCar.getStoreId()!=null){
                    Store store = storeMapper.selectStoreById(agentStoreCar.getStoreId());
                    reimbursementsSearchVo.setUserId(store.getMid());
                    List<ReimbursementsView> reimbursementsViews = reimbursementsMapper.selectReimbursementsList(reimbursementsSearchVo);
                    reimbursementsViewList.addAll(reimbursementsViews);
                } else if (agentStoreCar.getCarId() != null) {
                    Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(agentStoreCar.getCarId());
                    reimbursementsSearchVo.setUserId(vehicles.getUserId());
                    List<ReimbursementsView> reimbursementsViews = reimbursementsMapper.selectReimbursementsList(reimbursementsSearchVo);
                    reimbursementsViewList.addAll(reimbursementsViews);
                }
            }
        }else {
            reimbursementsViewList = reimbursementsMapper.selectReimbursementsList(reimbursementsSearchVo);
        }
        return reimbursementsViewList;
    }

    /**
     * 报销审核
     *
     * @param reimbursements 报销
     * @return
     */
    @Override
    public R updateReimbursements(ReimbursementsVo reimbursements) {
        return reimbursementsMapper.updateReimbursements(reimbursements) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    @Override
    public R deleteReimbursementsById(String id) {
        return reimbursementsMapper.deleteReimbursementsById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteReimbursementsByIds(String[] id) {
        return reimbursementsMapper.deleteReimbursementsByIds(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
